package com.xin.appaspectj2;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import static android.content.ContentValues.TAG;

/**
 * @version [AspectJDemo, 17/1/12 14:17]
 */
@Aspect
public class PermissionAspect {
    @Around("set(String com.xin.appaspectj2.MainActivity.string)")
    public void aroundFieldSet(ProceedingJoinPoint joinPoint) throws Exception {
        log("aroundFieldSet() called with: joinPoint = [" + joinPoint + "]");
    }

    @Around("get(String com.xin.appaspectj2.MainActivity.string)")
    public String aroundFieldGet(ProceedingJoinPoint joinPoint) throws Throwable {
        // 执行原代码
        Object obj = joinPoint.proceed();
        String string = (String) obj;
        log("aroundFieldGet() called with: joinPoint = [" + joinPoint + "] " + string);
        return "100";
    }

    private void log(String s) {
        Log.e("PermissionAspect", s);
    }

    @Pointcut("call(* com.xin.appaspectj2.MainActivity.setString(..))")
    public void callMethod() {
    }

    @Before("callMethod()")
    public void callMethodBefore(final ProceedingJoinPoint joinPoint, MPermission permission) throws Throwable {
        log("callMethodBefore() called with: joinPoint = [" + joinPoint + "], permission = [" + permission + "]");
    }

    @After("callMethod()")
    public void callMethodAfter(final ProceedingJoinPoint joinPoint, MPermission permission) throws Throwable {
        log("callMethodAfter() called with: joinPoint = [" + joinPoint + "], permission = [" + permission + "]");
    }

    /**
     * 定义切面
     *
     * @param permission
     */
    @Pointcut("execution(@com.xin.appaspectj2.MPermission * *(..)) && @annotation(permission)")
    public void methodAnnotatedWithMPermission(MPermission permission) {
    }

    @Around("methodAnnotatedWithMPermission(permission)")
    public void checkPermission(final ProceedingJoinPoint joinPoint, MPermission permission) throws Throwable {
        log("checkPermission() called with: joinPoint = [" + joinPoint + "], permission = [" + permission + "]");
        // 权限
        String permissionStr = permission.value();

        // 模拟权限申请
        MainActivity mainActivity = (MainActivity) joinPoint.getThis();  // 一般使用栈顶Activity作为上下文
        new AlertDialog.Builder(mainActivity).setTitle("提示")
                .setMessage(permissionStr)
                .setNegativeButton("取消", null)
                .setPositiveButton("允许", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            // 继续执行原方法
                            joinPoint.proceed();
                        } catch (Throwable throwable) {
                            throwable.printStackTrace();
                        }

                    }
                }).create().show();
    }
}
